package com.cskaoyan.controller;

import com.cskaoyan.annotation.ExecutionLog;
import com.cskaoyan.bean.bo.wjjBO.*;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.marketBean.MarketAd;
import com.cskaoyan.bean.marketBean.MarketCoupon;
import com.cskaoyan.bean.marketBean.MarketTopic;
import com.cskaoyan.bean.vo.wjjVO.*;
import com.cskaoyan.service.PromotionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author wjj
 * @date 2022-12-28 15:14
 * @describe 推广管理模块
 */

@RequestMapping("admin")
@RestController
public class PromotionController {

    @Autowired
    PromotionService promotionService;

    /**
     * 推广模块 ---- 广告管理 ---- 查询广告
     * 请求网址: http://localhost:8083/admin/ad/list?page=1&limit=20&sort=add_time&order=desc
     * 请求方法: GET
     */

    @RequestMapping("ad/list")
    public BaseRespVo adList(PromotionAdListBO promotionAdListBO){
        PromotionAdListVO promotionAdListVO = promotionService.selectMarketAd(promotionAdListBO);
        BaseRespVo baseRespVo = BaseRespVo.ok(promotionAdListVO);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * 推广模块 ---- 广告管理 ---- 添加广告
     * 添加一个广告
     * 请求网址: http://182.92.235.201:8083/admin/ad/create
     * 请求方法: POST
     */

    @ExecutionLog
    @RequestMapping("ad/create")
    public BaseRespVo adCreate(@RequestBody @Valid PromotionAdCreateNewAdBO createNewAdBO, BindingResult bindingResult) {

        PromotionAdCreateAdAndUpdateAdVO newAdVO = promotionService.addNewAd(createNewAdBO);
        BaseRespVo baseRespVo = BaseRespVo.ok(newAdVO);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * 推广模块 ---- 广告管理 ---- 删除广告
     * 请求网址: http://182.92.235.201:8083/admin/ad/delete
     * 请求方法: POST
     */

    @RequestMapping("ad/delete")
    public BaseRespVo adDelete(@RequestBody PromotionAdDeleteBO promotionAdDeleteBO){
        promotionService.deleteAd(promotionAdDeleteBO);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * 推广模块 ---- 广告管理 ---- 修改广告
     * 请求网址: http://182.92.235.201:8083/admin/ad/update
     * 请求方法: POST
     */

    @ExecutionLog
    @RequestMapping("ad/update")
    public BaseRespVo adUpdate(@RequestBody @Valid PromotionAdUpdateBO promotionAdUpdateBO,BindingResult bindingResult){
        MarketAd adVO = promotionService.updateAd(promotionAdUpdateBO);
        BaseRespVo baseRespVo = BaseRespVo.ok(adVO);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * 推广模块 ---- 优惠券管理 ---- 查询优惠券
     * 请求网址: http://182.92.235.201:8083/admin/coupon/list?page=1&limit=20&sort=add_time&order=desc
     * 请求方法: GET
     */

    @RequestMapping("coupon/list")
    public BaseRespVo couponList(PromotionCouponListBO promotionCouponListBO){
        PromotionAdCouponListVO promotionAdCouponListVO = promotionService.selectCouponList(promotionCouponListBO);
        BaseRespVo baseRespVo = BaseRespVo.ok(promotionAdCouponListVO);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    /**
     * 推广模块 ---- 优惠券管理 ---- 添加新的优惠券
     * 请求网址: http://localhost:8083/admin/coupon/create
     * 请求方法: POST
     */

    @ExecutionLog
    @RequestMapping("coupon/create")
    public BaseRespVo create(@RequestBody @Valid PromotionCouponCreateBO couponCreateBO,BindingResult bindingResult){
        if (couponCreateBO.getTotal() < couponCreateBO.getLimit() && couponCreateBO.getTotal() != 0){
            BaseRespVo baseRespVo = BaseRespVo.exception("每人限领数量不能超过优惠券总数");
            return baseRespVo;
        }else {
            MarketCoupon marketCoupon = promotionService.addNewCoupon(couponCreateBO);
            BaseRespVo baseRespVo = BaseRespVo.ok(marketCoupon);
            baseRespVo.setErrno(0);
            baseRespVo.setErrmsg("成功");
            return baseRespVo;
        }
    }

    /**
     * 推广模块 ---- 优惠券管理 ---- 删除优惠券
     * 请求网址: http://localhost:8083/admin/coupon/delete
     * 请求方法: POST
     */

    @RequestMapping("coupon/delete")
    public BaseRespVo delete(@RequestBody Map map){
        int code = promotionService.deleteCoupon(map);

        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * 推广模块 ---- 优惠券管理 ---- 修改优惠券
     * 请求网址: http://localhost:8083/admin/coupon/update
     * 请求方法: POST
     */

    @ExecutionLog
    @RequestMapping("coupon/update")
    public BaseRespVo update(@RequestBody @Valid PromotionCouponUpdateBO couponUpdateBO,BindingResult bindingResult){
        MarketCoupon coupon = promotionService.updateCoupon(couponUpdateBO);

        BaseRespVo baseRespVo = BaseRespVo.ok(coupon);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * 推广模块 ---- 优惠券管理 ---- 修改优惠券
     * 请求网址: http://localhost:8083/admin/coupon/read?id=8
     * 请求方法: GET
     */

    @RequestMapping("coupon/read")
    public BaseRespVo read(Integer id){
        MarketCoupon coupon = promotionService.selectCouponById(id);

        BaseRespVo baseRespVo = BaseRespVo.ok(coupon);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * 推广模块 ---- 优惠券管理 ---- 优惠券详情使用者
     * 请求网址: http://localhost:8083/admin/coupon/listuser?page=1&limit=20&couponId=8&sort=add_time&order=desc
     * 请求方法: GET
     */

    @RequestMapping("coupon/listuser")
    public BaseRespVo listUser(PromotionCouponListUserBO couponListUserBO){
        PromotionAdCouponListUserVO couponListUserVO = promotionService.selectCouponUserById(couponListUserBO);

        BaseRespVo baseRespVo = BaseRespVo.ok(couponListUserVO);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * 推广模块 ---- 专题管理 ---- 页面展示
     * http://localhost:8083/admin/topic/list?page=1&limit=20&sort=add_time&order=desc
     * 请求方法: GET
     */

    @RequestMapping("topic/list")
    public BaseRespVo listTopic(PromotionTopicListBO topicListBO){
        PromotionTopicVO topicVO = promotionService.selectTopic(topicListBO);

        BaseRespVo baseRespVo = BaseRespVo.ok(topicVO);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * 推广模块 ---- 专题管理 ---- 添加一个专题
     * 请求网址: http://localhost:8083/admin/topic/create
     * 请求方法: POST
     */

    @ExecutionLog
    @RequestMapping("topic/create")
    public BaseRespVo createTopic(@RequestBody @Valid PromotionTopicCreateBO topicCreateBO,BindingResult bindingResult){
        String readCount = topicCreateBO.getReadCount();

        //阅读量的数额
        String ReadCountNum = readCount.substring(0, readCount.length() - 1);

        //阅读量的单位
        String ReadCountUnit = readCount.substring(readCount.length()-1);
        try {
            double count = Double.parseDouble(ReadCountNum);
            if ("k".equals(ReadCountUnit)){
                MarketTopic newTopic = promotionService.addTopic(topicCreateBO);
                BaseRespVo baseRespVo = BaseRespVo.ok(newTopic);
                baseRespVo.setErrmsg("成功");
                baseRespVo.setErrno(0);
                return baseRespVo;
            }
            return BaseRespVo.fail("阅读量格式必须为：数字+k，如：1k（k表示千）");
        }catch (Exception e){
            return BaseRespVo.fail("阅读量格式必须为：数字+k，如：1k（k表示千）");
        }
    }

    /**
     * 推广模块 ---- 专题管理 ---- 删除一个专题
     * 请求网址: http://localhost:8083/admin/topic/delete
     * 请求方法: POST
     */

    @RequestMapping("topic/delete")
    public BaseRespVo deleteTopic(@RequestBody PromotionTopicDeleteBO topicDeleteBO){
        promotionService.deleteTopic(topicDeleteBO);

        BaseRespVo baseRespVo = BaseRespVo.ok(null);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * 推广模块 ---- 专题管理 ---- 批量删除
     * 请求网址: http://localhost:8083/admin/topic/batch-delete
     * 请求方法: POST
     */

    @RequestMapping("topic/batch-delete")
    public BaseRespVo batchDeleteTopic(@RequestBody PromotionTopicBatchDeleteBO promotionTopicBatchDeleteBO){
        promotionService.batchDeleteTopic(promotionTopicBatchDeleteBO);

        BaseRespVo baseRespVo = BaseRespVo.ok(null);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * 推广模块 ---- 专题管理 ---- 修改专题 ---- 回显数据
     * 请求网址: http://localhost:8083/admin/topic/read?id=264
     * 请求方法: GET
     */

    @RequestMapping("topic/read")
    public BaseRespVo readTopic(Integer id){
        PromotionTopicReadVO topicReadVO = promotionService.readTopic(id);

        BaseRespVo baseRespVo = BaseRespVo.ok(topicReadVO);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * 推广模块 ---- 专题管理 ---- 修改专题
     * 请求网址: http://localhost:8083/admin/topic/update
     * 请求方法: POST
     */

    @ExecutionLog
    @RequestMapping("topic/update")
    public BaseRespVo updateTopic(@RequestBody @Valid PromotionTopicUpdateBO topicUpdateBO,BindingResult bindingResult){
        String readCount = topicUpdateBO.getReadCount();

        //阅读量的数额
        String ReadCountNum = readCount.substring(0, readCount.length() - 1);

        //阅读量的单位
        String ReadCountUnit = readCount.substring(readCount.length()-1);
        try {
            double count = Double.parseDouble(ReadCountNum);
            if ("k".equals(ReadCountUnit)){
                MarketTopic topic = promotionService.updateTopic(topicUpdateBO);
                BaseRespVo baseRespVo = BaseRespVo.ok(topic);
                baseRespVo.setErrmsg("成功");
                baseRespVo.setErrno(0);
                return baseRespVo;
            }
            return BaseRespVo.fail("阅读量格式必须为：数字+k，如：1k（k表示千）");
        }catch (Exception e){
            return BaseRespVo.fail("阅读量格式必须为：数字+k，如：1k（k表示千）");
        }
    }


    @RequestMapping("collect/list")
    public BaseRespVo collectList(WJJCollectListBO collectListBO){
        WJJUserCollectListVO collect = promotionService.selectCollect(collectListBO);

        BaseRespVo baseRespVo = BaseRespVo.ok(collect);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    /**
     * 请求网址: http://localhost:8083/admin/footprint/list?page=1&limit=20&sort=add_time&order=desc
     * 请求方法: GET
     * @param
     * @return
     */
    @RequestMapping("footprint/list")
    public BaseRespVo footprintList(WJJFootprintListBO footprintListBO){
        WJJFootprintListVO footprintListVO = promotionService.selectFootprint(footprintListBO);

        BaseRespVo baseRespVo = BaseRespVo.ok(footprintListVO);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }
}
